﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Essensoft.AspNetCore.Payment.Alipay.Domain;
using Essensoft.AspNetCore.Payment.Alipay.Request;
using Essensoft.Paylink.WeChatPay.V2;
using Essensoft.Paylink.WeChatPay.V3.Request;
using Microsoft.Extensions.Options;
using ViazyNetCore.Modules.Payment.Events;
using ViazyNetCore.Modules.ShopMall;

namespace ViazyNetCore.Modules.Payment.WechatPay.Events
{
    public class WechatPaymentHandler : IPaymentHandler
    {
        private readonly IWeChatPayClient _client;
        private readonly IWeChatPayNotifyClient _weChatPayNotifyClient;
        private readonly WechatPayMiddlewareOptions _options;

        public Buyway Buyway => Buyway.WxPay;
        public WechatPaymentHandler(IWeChatPayClient weChatPayClient
            , IWeChatPayNotifyClient weChatPayNotifyClient
            , IOptions<WechatPayMiddlewareOptions> options)
        {
            this._client = weChatPayClient;
            this._weChatPayNotifyClient = weChatPayNotifyClient;
            this._options = options.Value;
        }

        public Task<PayCreateValue> CreatePayment(PayMediaType payMediaType, TradeViewModel trade)
        {
            //switch (payMediaType)
            //{
            //    case PayMediaType.PC:
            //        return await PagePay(tradeModel);
            //    case PayMediaType.WAP:
            //        return await WapPay(tradeModel);
            //    case PayMediaType.APP:
            //        return await AppPay(tradeModel);
            //    case PayMediaType.WeiXinMP:
            //    default:
            //        break;
            //};
            throw new NotImplementedException();
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="viewModel"></param>
        ///// <returns></returns>
        //public async Task<PayCreateValue> PagePay(TradeViewModel viewModel)
        //{
        //    var model = new WeChatPayAppSdkRequest
        //    {
        //        Body = viewModel.Body,
        //        Subject = viewModel.Subject,
        //        TotalAmount = viewModel.TotalAmount.ToString("0.00"),
        //        OutTradeNo = viewModel.OutTradeNo,
        //        ProductCode = "FAST_INSTANT_TRADE_PAY"
        //    };
        //    var req = new WeChatPayAppSdkRequest();
        //    req.SetBizModel(model);

        //    var notifyUrl = GA.UrlCombine(this._options.Host, this._options.NotifyUrl, this._options.PageNotifyRoute);

        //    req.SetNotifyUrl(notifyUrl);
        //    req.SetReturnUrl(viewModel.ReturnUrl);

        //    var response = await _client.PageExecuteAsync(req);

        //    var payCreateValue = new PayCreateValue()
        //    {
        //        BodyOrderId = viewModel.OutTradeNo,
        //        Content = response.Body,
        //        ContentType = PayContentType.Html
        //    };
        //    return payCreateValue;
        //}
        //public async Task<PayCreateValue> WapPay(TradeViewModel viewModel)
        //{
        //    var model = new AlipayTradeWapPayModel
        //    {
        //        Body = viewModel.Body,
        //        Subject = viewModel.Subject,
        //        TotalAmount = viewModel.TotalAmount.ToString("0.00"),
        //        OutTradeNo = viewModel.OutTradeNo,
        //        ProductCode = "QUICK_WAP_WAY"
        //    };
        //    var req = new WeChatPayJsApiSdkRequest();

        //    var notifyUrl = GA.UrlCombine(this._options.Host, this._options.NotifyUrl, this._options.WapNotifyRoute);

        //    req.SetNotifyUrl(notifyUrl);
        //    req.SetReturnUrl(viewModel.ReturnUrl);

        //    var response = await _client.PageExecuteAsync(req);

        //    var payCreateValue = new PayCreateValue()
        //    {
        //        BodyOrderId = viewModel.OutTradeNo,
        //        Content = response.Body,
        //        ContentType = PayContentType.Html
        //    };
        //    return payCreateValue;
        //}

        //public async Task<PayCreateValue> AppPay(TradeViewModel viewModel)
        //{
        //    var model = new AlipayTradeAppPayModel
        //    {
        //        OutTradeNo = viewModel.OutTradeNo,
        //        Subject = viewModel.Subject,
        //        TotalAmount = viewModel.TotalAmount.ToString("0.00"),
        //        Body = viewModel.Body,
        //        ProductCode = "QUICK_MSECURITY_PAY"
        //    };
        //    var req = new AlipayTradeAppPayRequest();

        //    var notifyUrl = GA.UrlCombine(this._options.Host, this._options.NotifyUrl, this._options.AppNotifyRoute);

        //    req.SetBizModel(model);
        //    req.SetNotifyUrl(notifyUrl);

        //    var response = await _client.(req);

        //    var payCreateValue = new PayCreateValue()
        //    {
        //        BodyOrderId = viewModel.OutTradeNo,
        //        Content = response.Body,
        //        ContentType = PayContentType.Scripts
        //    };
        //    return payCreateValue;
        //}

    }
}
